package com.easycarebn.main.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.easycarebn.main.utils.DownloadAttach;
import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.controller.ServerController;
import com.easycarebn.request.data.DoctorDetail;
import com.easycarebn.request.data.NotificationItem;
import com.easycarebn.request.data.UserResponse;
import com.easycarebn.service.request.OnRequest;
import com.squareup.picasso.Picasso;
import com.timbacsi.main.DetailtActivity;
import com.timbacsi.main.R;

public class FragmentMessageDetailt extends Fragment {

	private View mView;
	private ImageView mImgAvatar;
	private TextView mTvSentBy;
	private TextView mTvTime;
	private TextView mTvContent;
	private TextView mAttach;

	private TextView mTvChitiet;
	private String shortName;

	private static FragmentMessageDetailt fragment = null;
	private IResult callIResult;
	private String mToken = null;
	private NotificationItem mData;
	private Button btnTraloi;
	ProgressBar progressBar;

	public static FragmentMessageDetailt newInstance(String title, Bundle data) {
		fragment = new FragmentMessageDetailt();
		fragment.setArguments(data);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof IResult) {
			callIResult = (IResult) activity;
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mView = inflater.inflate(R.layout.fragment_message_detailt, null);
		mImgAvatar = (ImageView) mView.findViewById(R.id.img_avatar);
		mTvContent = (TextView) mView.findViewById(R.id.tv_content);
		mTvSentBy = (TextView) mView.findViewById(R.id.tv_sent_by);
		mTvTime = (TextView) mView.findViewById(R.id.tv_time);
		mTvChitiet = (TextView) mView.findViewById(R.id.tv_chitiet);
		mAttach = (TextView) mView.findViewById(R.id.tv_dinhkem);
		btnTraloi = (Button) mView.findViewById(R.id.btnTraloi);
		btnTraloi.setVisibility(View.GONE);
		btnTraloi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				UserResponse userResponse = Utils.getUserLogin(getActivity());
				if (userResponse != null) {
					DoctorDetail docter = new DoctorDetail();
					docter.setName(mData.getSent_by_name());
					docter.setDoctor_id(mData.getSent_by());
					Utils.sendMessagesToDocter((Activity) getActivity(),
							userResponse.getToken(), docter,
							mData.getReply_for_notification_id());
				}
			}
		});
		mAttach.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Utils.showMessage(getActivity(),
						getResources().getString(R.string.bat_dau_tai_file));
				new DownloadAttach(getActivity(), shortName, 1).execute(mData
						.getAttachs().getFileName());
			}
		});

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		callIResult.onDisplay(Utils.TASK_CHILD, "");
		UserResponse user = Utils.getUserLogin(getActivity());
		if (user != null) {
			mToken = user.getToken();
		} else {
			mToken = null;
			Utils.showMessage(getActivity(),
					getResources().getString(R.string.vui_long_dang_nhap));
			return;
		}
		DetailtActivity.loadding.setVisibility(View.VISIBLE);
		ServerController.getInstance(getActivity()).requestNotificationDetail(
				mToken,
				Integer.parseInt(fragment.getArguments()
						.getString(Utils.KEY_ID)),
				new OnRequest<NotificationItem>() {

					@Override
					public void onSuccess(NotificationItem data) {
						mData = data;
						DetailtActivity.loadding.setVisibility(View.GONE);
						Picasso.with(getActivity())
								.load(data.getSent_by_avatar_thumbs())
								.placeholder(R.drawable.avata)
								.error(R.drawable.avata).into(mImgAvatar);

						mTvContent.setText(Html.fromHtml(data.getContent()));
						if (TextUtils.isEmpty(data.getSent_by_name())) {
							Utils.setTextEmpty(mTvSentBy, getActivity());
						} else {
							mTvSentBy.setText(Html.fromHtml(data
									.getSent_by_name()));
						}
						mTvTime.setText(String.format(getActivity()
								.getResources().getString(R.string.gui_ngay),
								data.getCreatedAt()));

						if (data.getReply_for_notification_id() != -1) {
							btnTraloi.setVisibility(View.VISIBLE);
						} else {
							btnTraloi.setVisibility(View.GONE);
						}

						if (data.getAppointment_id() > 0) {
							mTvChitiet.setVisibility(View.VISIBLE);
						}
						if (data.getAttachs() != null
								&& !data.getAttachs().getFileName().isEmpty()) {
							mAttach.setVisibility(View.VISIBLE);
							String fileName[] = data.getAttachs().getFileName()
									.split("\\/");
							shortName = fileName[fileName.length - 1];
							String dinhkem = String.format(getResources()
									.getString(R.string.dinh_kem_data),
									shortName);

							mAttach.setText(dinhkem);
						}
					}

					@Override
					public void onError(int errorCode, String message) {
						// TODO Auto-generated method stub

					}
				});

		mTvChitiet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString(Utils.KEY_ID, mData.getAppointment_id() + "");
				Utils.intentToDetailt(getActivity(), Utils.APPOINTMENT_DETAIL,
						bundle);
			}
		});

	}
}
